package com.yiqi.liebang.feature.mine.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baiiu.filter.ClassifyBo;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.filter.entity.FilterType;
import com.yiqi.liebang.entity.bo.CouponBo;
import com.yiqi.liebang.feature.home.view.QuestionListActivity;
import com.yiqi.liebang.feature.mine.contract.CouponContract;
import com.yiqi.liebang.feature.mine.dependencies.coupon.CouponPresenterModule;
import com.yiqi.liebang.feature.mine.dependencies.coupon.DaggerCouponComponent;
import com.yiqi.liebang.feature.mine.view.adapter.CouponAdpter;
import com.yiqi.liebang.framework.DataServer;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * A simple {@link Fragment} subclass.
 */
public class CouponFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.OnItemChildClickListener, OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, CouponContract.View {
    @Inject
    CouponContract.Presenter mPresenter;

    @BindView(R.id.rv_coupon)
    RecyclerView mRvCoupon;
    Unbinder unbinder;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    private int mType = 0;
    private CouponAdpter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<CouponBo> mData;
    private int mCurrentCounter = 0;

    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private String mLabelsStr;


    public static CouponFragment getInstance(int mType) {
        CouponFragment sf = new CouponFragment();
        sf.mType = mType;
        return sf;
    }


    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_coupon;
    }

    @Override
    protected void initInjector() {
        DaggerCouponComponent.builder().couponPresenterModule(new CouponPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initAdapter(mType);
    }

    @Override
    public void onStart() {
        super.onStart();

    }


    @Override
    protected void initData() {
        pageIndex = 1;
        mLabelsStr = getActivity().getIntent().getStringExtra("ask_lables");
        mPresenter.getCoupon(mType, pageIndex, pageSize, mLabelsStr);
    }

    @Override
    protected void initEvent() {

    }

    private void initAdapter(int type) {
        mAdapter = new CouponAdpter(type);
        mEmptyView = new EmptyView(mRvCoupon);
        mRvCoupon.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter.bindToRecyclerView(mRvCoupon);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mAdapter.setOnLoadMoreListener(this);
//        mAdapter.setNewData(DataServer.getCouponBoData(20));

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (mAdapter.getData().get(position).getClassifyId().equals("1")){
            toList();
        }else {
            Intent intents = new Intent(getActivity(), QuestionListActivity.class);
            intents.putExtra("type_id",mAdapter.getData().get(position).getClassifyId());
            intents.putExtra("type_name", mAdapter.getData().get(position).getClassify());
            startActivity(intents);
        }


    }

    public  void toList(){
        AM.api().createApiService(HomeApi.class)
                .getAllClassify()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<ClassifyBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<ClassifyBo> classifyBos) {
                        Intent intents = new Intent(getActivity(), QuestionListActivity.class);
                        intents.putExtra("type_id",classifyBos.get(0).getId());
                        intents.putExtra("type_name", classifyBos.get(0).getClassify());
                        startActivity(intents);
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.showShort(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    private void refresh() {
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        initData();
    }

    private void loadMore() {
        mPresenter.getCouponMore(mType, pageIndex, pageSize, mLabelsStr);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showCouponSuccess(List<CouponBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showCouponError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
    }

    @Override
    public void showCouponMoreSuccess(List<CouponBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showCouponMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showCouponEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }
}
